package com.yfp.operation.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.yfp.common.core.utils.StringUtils;
import com.yfp.operation.config.WxMaConfiguration;
import com.yfp.operation.config.WxPayProperties;
import com.yfp.operation.domain.Client;
import com.yfp.operation.domain.ClientCoupon;
import com.yfp.operation.enums.CouponTypeEnum;
import com.yfp.operation.service.ClientService;
import com.yfp.operation.service.WxInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WxInfoServiceImpl implements WxInfoService {

    @Resource
    private WxPayProperties wxPayProperties;
    @Resource
    private ClientService clientService;

    @Async("threadPoolTaskExecutor")
    @Override
    public void notifyCouponArriveReminder(List<String> notifyCouponArriveReminderList, ClientCoupon clientCoupon) {
        log.info("notifyCouponArriveReminder,notifyCouponArriveReminderList:{},ClientCoupon:{}", notifyCouponArriveReminderList, clientCoupon);
        List<Client> clients = clientService.listByUserIds(notifyCouponArriveReminderList);

        // 组装消息
        HashMap<String, String> sendMsgData = this.assembleCouponArriveReminder(clientCoupon);
        for (Client client : clients) {
            this.sendSubscribeMsg(client.getOpenId(), "WiLg6P_YrOaHXH8wj7BrYmqIkydnFkDfCm_7ymDnT2E", sendMsgData);
        }
    }

    @Override
    public HashMap<String, String> assembleCouponArriveReminder(ClientCoupon clientCoupon) {
        // 定向给用户发送优惠券到账提醒
        HashMap<String, String> sendMsgData = MapUtil.newHashMap();
        // 优惠券名称
        sendMsgData.put("thing1", StringUtils.limitString(clientCoupon.getCouponName(), 20));
        // 过期时间
        sendMsgData.put("time2", DateUtil.formatDateTime(clientCoupon.getValidEndTime()));
        // 优惠券类型
        sendMsgData.put("thing3", StringUtils.limitString(CouponTypeEnum.getDescByCode(clientCoupon.getType()), 20));
        // 温馨提示
        sendMsgData.put("thing4", StringUtils.limitString("您的优惠券已到账！", 20));
        return sendMsgData;
    }

    @Override
    public void sendSubscribeMsg(String toUser, String templateId, HashMap<String, String> sendMsgData) {
        log.info("sendSubscribeMsg,toUser:{},templateId:{},sendMsgData:{}", toUser, templateId, sendMsgData);
        WxMaService wxService = WxMaConfiguration.getMaService(wxPayProperties.getConfigs().get(0).getAppId());
        WxMaSubscribeService subscribeService = wxService.getSubscribeService();

        List<WxMaSubscribeMessage.MsgData> msgDataList = sendMsgData.entrySet().stream().map(entry -> new WxMaSubscribeMessage.MsgData(entry.getKey(), entry.getValue())).collect(Collectors.toList());
        WxMaSubscribeMessage build = WxMaSubscribeMessage.builder().toUser(toUser).templateId(templateId).data(msgDataList).build();
        try {
            subscribeService.sendSubscribeMsg(build);
        } catch (WxErrorException e) {
            log.error("sendSubscribeMsg:{}", e.getMessage());
        }
    }

}
